package scene.gameObject 
{
	import event.CommEvent;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	import resource.ImageResource;
	import scene.scene.SceneData;
	import utils.clientConst;
	/**
	 * ...
	 * @author carlor
	 */
	public class BasicObject extends Sprite
	{
		protected var _owner	  : SceneData;
		protected var _basicData  : BasicData;
		protected var _position   : Point; //sceneposition
		protected var _positionChanged: Boolean;
		
		protected var _imageArray : Object;
		protected var _bitmap     : Bitmap;
		protected var _centerPoint: Point;
		
		public function BasicObject(owner: SceneData, data: Object, settingData: Object) 
		{
			_owner = owner;
			_basicData = new BasicData(data);
			_position  = new Point();
			init(settingData);
		}
		
		public function dispose(): void
		{
			_basicData.dispose();
			_basicData = null;
			if (_bitmap && _bitmap.bitmapData) {
				_bitmap.bitmapData.dispose();
				_bitmap.bitmapData = null;
			}
			_bitmap = null;
			_position = null;
			
			for (var key: Object in _imageArray) {
				Engine.resourceManager.unloadResource(_imageArray[key].fileName, ImageResource);
			}
			_imageArray = null;
		}
		
		public function update(curTime: int): void
		{
			
		}		
		
		protected function loadImages(): void
		{
		}
		
		protected function onResourceLoaded(r: ImageResource): void
		{
			if (r.fileName.indexOf("stand") >= 0) _imageArray[ActionConst.ACTION_STAND] = r;
			else if (r.fileName.indexOf("move") >= 0) _imageArray[ActionConst.ACTION_MOVE] = r;
			else if (r.fileName.indexOf("attack") >= 0) _imageArray[ActionConst.ACTION_ATTACK] = r;
			else if (r.fileName.indexOf("skill") >= 0) _imageArray[ActionConst.ACTION_SKILL] = r; 
			else if (r.fileName.indexOf("defense") >= 0) _imageArray[ActionConst.ACTION_DEFENSE] = r; 
		}
		
		protected function onResourceLoadFailed(r: ImageResource): void
		{
			if (r.fileName.indexOf("stand") >= 0) _imageArray[ActionConst.ACTION_STAND] = null;
			else if (r.fileName.indexOf("move") >= 0) _imageArray[ActionConst.ACTION_MOVE] = null;
			else if (r.fileName.indexOf("attack") >= 0) _imageArray[ActionConst.ACTION_ATTACK] = null;
			else if (r.fileName.indexOf("skill") >= 0) _imageArray[ActionConst.ACTION_SKILL] = null; 
			else if (r.fileName.indexOf("defense") >= 0) _imageArray[ActionConst.ACTION_DEFENSE] = null; 
		}
		
		protected function init(settingData: Object): void
		{
			_bitmap = new Bitmap();
			_centerPoint = new Point();
			_imageArray = new Array();
			loadImages();
			this.addChild(_bitmap);
			setMapPos(_basicData.mapPos.x, _basicData.mapPos.y);
		}
		
		public function setMapPos(x: int, y: int): void
		{
			Engine.dialogManager.handlePacket("onShowMessageInfo", basicData.name+"移动到("+x+","+y+")");
			
			var oldPos: Point = new Point(_basicData.mapPos.x, _basicData.mapPos.y);
			_basicData.mapPos.x = x;
			_basicData.mapPos.y = y;
			this.dispatchEvent(new CommEvent(CommEvent.MAPPOS_CHANGE).addData("oldPos", oldPos).addData("newPos", _basicData.mapPos));
			setPosition(x * clientConst.CELLWIDTH, y * clientConst.CELLHEIGHT);
		}
		
		public function setPosition(x: int, y: int): void
		{
			_position.x = x;
			_position.y = y;
			//if (this.basicData.name.indexOf("carlor") >= 0) {
			//	trace("set position" + _position.y);
			//}
			this.dispatchEvent(new CommEvent(CommEvent.POSITION_CHANGE));
			

			var np: Point = _owner.scenePos2ScreenPos(_position);
			this.x = np.x + clientConst.CELLWIDTH/2 - _centerPoint.x;
			this.y = np.y + clientConst.CELLHEIGHT / 2 - _centerPoint.y;		
			
			_positionChanged = true;
		}
		
		public function resize(): void
		{
			var oldx: Number = this.x;
			
			var np: Point = _owner.scenePos2ScreenPos(_position);
			this.x = np.x + clientConst.CELLWIDTH/2 - _centerPoint.x;
			this.y = np.y + clientConst.CELLHEIGHT / 2 - _centerPoint.y;		
		}
		
		public function get basicData(): BasicData
		{
			return _basicData;
		}
		
		public function get bitmap(): Bitmap
		{
			return _bitmap;
		}
		
		public function get centerPoint(): Point
		{
			return _centerPoint;
		}
		
		public function get position(): Point
		{
			return _position;
		}
		
		public function get positionChanged(): Boolean
		{
			var changed: Boolean = _positionChanged;
			_positionChanged = false;
			return changed;
		}
	}

}